package liuch;

/**
 * 〈一句话功能简述〉<br> 〈功能详细描述〉
 *
 * @author 17101651 on 2018/5/20.
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class VolatileTest {
    private static volatile int race = 0;

    public static void increase() {
        race++;
    }

    private static final int THREADS_COUNT = 20;

    public static void main(String[] args) {
        Thread[] threads = new Thread[THREADS_COUNT];
        for (int i = 0; i < THREADS_COUNT; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        increase();
                    }
                }
            });
            threads[i].start();
        }
        while (Thread.activeCount() > 2) {
            Thread.yield();
            System.out.println(race);
        }
    }
}
